trivial-httpd: Daemonize better
authorColin Walters <walters@verbum.org>
Thu, 12 Jan 2017 15:02:23 +0000 (10:02 -0500)
committerAtomic Bot <atomic-devel@projectatomic.io>
Thu, 12 Jan 2017 15:26:01 +0000 (15:26 +0000)
I was trying to debug `test-pull-c`, and typing `Ctrl-C` in gdb
ended up sending `SIGINT` to trivial-httpd as well, killing it.

Daemonize a bit more properly to avoid this. I also followed the standard
`/dev/null` guidelines.

Closes: #643
Approved by: jlebon

src/ostree/ostree-trivial-httpd.c

index ef297af6a24522c956775065420bdc98eca7c2e5..ddcb2fc03f4dd66a97c96a79fe8ba1df4493eae1 100644 (file)
@@ -30,6 +30,7 @@
 #include "otutil.h"
 
 #include <locale.h>
+#include <err.h>
 #include <sys/socket.h>
 #include <sys/prctl.h>
 #include <signal.h>
@@ -597,9 +598,12 @@ run (int argc, char **argv, GCancellable *cancellable, GError **error)
           goto out;
         }
       /* Child, continue */
+      if (setsid () < 0)
+        err (1, "setsid");
       /* Daemonising: close stdout/stderr so $() et al work on us */
-      fclose (stdout);
-      fclose (stdin);
+      freopen("/dev/null", "r", stdin);
+      freopen("/dev/null", "w", stdout);
+      freopen("/dev/null", "w", stderr);
     }
   else
     {